package com.anxin.act.config.service;

import java.util.ArrayList;
import java.util.List;

import com.anxin.act.config.dao.WfApplyFormFieldDao;
import com.anxin.act.config.entity.WfApplyFormField;
import com.anxin.common.utils.StringUtils;
import com.anxin.desform.dao.OnlCgformHeadDao;
import com.anxin.desform.entity.DesignFormData;
import com.anxin.desform.entity.OnlCgformHead;
import com.anxin.desform.service.DesignFormDataService;
import com.anxin.framework.web.entity.AjaxResult;
import com.anxin.framework.web.service.BaseService;
import com.anxin.common.utils.SecurityUtils;
import com.anxin.sys.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.anxin.act.config.dao.WfApplyFormDao;
import com.anxin.act.config.entity.WfApplyForm;

/**
 * 流程申请单Service业务层处理
 *
 * @author 刘翔宇
 * @date 2020-03-13
 */
@Service
public class WfApplyFormService extends BaseService<WfApplyFormDao,WfApplyForm>{
    @Autowired
    private WfApplyFormDao wfApplyFormDao;

    @Autowired
    private WfApplyFormFieldDao wfApplyFormFieldDao;
    @Autowired
    private OnlCgformHeadDao onlCgformHeadDao;
    @Autowired
    private DesignFormDataService designFormDataService;

    /**
     * 查询流程申请单
     *
     * @param id 流程申请单ID
     * @return 流程申请单
     */
    public WfApplyForm getById(String id){
        WfApplyForm wfApplyForm = wfApplyFormDao.getById(id);
        wfApplyForm.setWfApplyFormFieldList(wfApplyFormFieldDao.getByApplyFormId(id));
        return wfApplyForm;
    }

    /**
     * 查询流程申请单列表
     *
     * @param wfApplyForm 流程申请单
     * @return 流程申请单
     */
    public List<WfApplyForm> findList(WfApplyForm wfApplyForm){
        List<WfApplyForm> list = new ArrayList<>();
        if (StringUtils.isNotNull(wfApplyForm)){
            list = wfApplyFormDao.findList(wfApplyForm);
        }
        return list;
    }

    /**
     * 保存流程申请单
     * @param wfApplyForm
     */
    public int save(WfApplyForm wfApplyForm){
        int row = super.save(wfApplyForm);

        //删除之前的表单字段
        wfApplyFormFieldDao.deleteByApplyFormId(wfApplyForm.getId());

        //保存表单字段
        for (WfApplyFormField wfApplyFormField : wfApplyForm.getWfApplyFormFieldList()) {
            wfApplyFormField.preInsert();
            wfApplyFormField.setApplyFormId(wfApplyForm.getId());
            wfApplyFormFieldDao.insert(wfApplyFormField);
        }

        return row;
    }


    /**
     * 保存流程申请单
     *
     * @param wfApplyForm 流程申请单
     * @return 结果
     */
    public int update(WfApplyForm wfApplyForm){
        return super.save(wfApplyForm);
    }


    /**
     * 删除流程申请单信息
     *
     * @param id 流程申请单ID
     * @return 结果
     */
    public int deleteById(String id){
        SysUser user = SecurityUtils.getUser();
        if(user == null){
            return 0;
        }
        return wfApplyFormDao.deleteById(id,user.getId());
    }

    /**
     * 根据表单类型获取表单集合
     * @param formType 表单类型
     * @return
     */
    public AjaxResult getFormList(String formType){
        List formList = null;

        switch (formType){
            case "dev":{
                formList = wfApplyFormDao.findList(new WfApplyForm());
                break;
            } case "form":{
                formList = wfApplyFormDao.findFormList();
                break;
            }case "defined":{
                formList = onlCgformHeadDao.selectTableBySingleOrMaster();
                for (Object item : formList) {
                    OnlCgformHead cgformHead=(OnlCgformHead) item;
                    String tempStr = "";
                    if(cgformHead.getTableType()==2){
                        tempStr = cgformHead.getTableTxt()+"(主表)";
                    }else if(cgformHead.getTableType()==1){
                        tempStr = cgformHead.getTableTxt()+"(单表)";
                    }
                    cgformHead.setName(tempStr);
                }
                break;
            }case "design":{
                DesignFormData data=new DesignFormData();
                formList= designFormDataService.findList(data);
                formList.forEach(e->{  ((DesignFormData)e).setName(((DesignFormData) e).getDesformName());  });
                break;
            }
        }
        return AjaxResult.success(formList);
    }


}
